package de.koller.worlders.engine.entity.zone;

import de.koller.worlders.engine.entity.mo.MovingObject;
import de.koller.worlders.engine.tiles.ITile;
import de.koller.worlders.engine.tiles.data.ITileCreator;
import de.koller.worlders.engine.tiles.data.MapDetails;
import de.koller.worlders.render.VisualMap;
import de.koller.worlders.render.visual.DisplayRenderObject;

public class MapLeave extends ZoneEffect {
	public static final int GEN_ID = 12;
	
	private String loadName;
	private long lastEnter;
	
	public static final ITileCreator GEN_INSTANCE = new ITileCreator() {
		@Override public ITile newInstance() {
			return new Zone().addEffect( new MapLeave( false ) );
		}
		
		@Override public DisplayRenderObject getIcon() { return VisualMap.leave; }
		@Override public String getName() { return "Weltportal"; }
	};
	
	public static final ITileCreator GEN_INSTANCE_1 = new ITileCreator() {
		@Override public ITile newInstance() {
			return new Zone().addEffect( new MapLeave( true ) );
		}
		
		@Override public DisplayRenderObject getIcon() { return VisualMap.story; }
		@Override public String getName() { return "Weltportal (Storymode)"; }
	};
	
	private final boolean storyLeave;

	public MapLeave( boolean storyLeave ) {
		this.storyLeave = storyLeave;
	}
	
	@Override
	public void init( Zone z ) {
		super.init( z );
		
		if( storyLeave )
			z.getTile().setVisual( VisualMap.story );
		else
			z.getTile().setVisual( VisualMap.leave );
	}
	
	@Override
	public void enter( MovingObject mo ) {
		super.enter( mo );
		
		if( mo.mostate.isGameplayAffected() == false )
			return;
		
		lastEnter = zone.root.getTime();
		
		redefine();
		String dest = destination();
		if( dest == null ) {
			redefine();
			dest = destination();
		}

		if( dest != null ) {
			zone.root.getGui().getMessage().show( "loading..." );
		}
		
		this.loadName = dest;
	}

	@Override
	public void in( MovingObject mo ) {
		super.in( mo );
		
		if( mo.mostate.isGameplayAffected() == false )
			return;
		
		if( lastEnter != zone.root.getTime() && loadName != null ) {
			this.zone.root.getTileMap().reloadMap( loadName, storyLeave );
			zone.root.getGui().getMessage().unShow();
		}
	}

	@Override
	public void leave( MovingObject mo ) {
		super.leave( mo );
		
		zone.root.getGui().getMessage().unShow();
	}
	
	private String destination() {
		MapDetails d = zone.root.getTileMap().tileHolder.getDetail( zone.getPosition().getXi(), zone.getPosition().getYi() );
		MapDetails.DetailEntity mld = d.getDetail( MapDetails.IDEntity.MapLeave,
				zone.getPosition().getXi(), zone.getPosition().getYi() );
		
		if( mld == null )
			return null;
		else
			return mld.content;
	}
	
	private void redefine() {
		String n = zone.root.getGui().getConsoleInput().getContent();
		if( n.startsWith( "mapEnter " ) ) {
			n = n.replaceAll( "mapEnter ", "" );
			zone.root.getGui().getConsoleInput().clearConent();
		} else
			return;
		
		int x = zone.getPosition().getXi();
		int y = zone.getPosition().getYi();
		MapDetails d = zone.root.getTileMap().tileHolder.getDetail( zone.getPosition().getXi(), zone.getPosition().getYi() );
		
		d.removeDetail( MapDetails.IDEntity.MapLeave, x, y );
		
		d.addDetail( MapDetails.IDEntity.MapLeave, x, y, n );
	}
}
